#if defined _td_store_included
	#endinput
#endif

#define _td_store_included
#define STORE_NO_LIMIT -1
#define STORE_LIMIT_GLOBAL -1

// This stores information on how many items of each type player has in his store
new playerStoreItems[MAX_PLAYERS][ITEMS];
new playerStoreLimits[MAX_PLAYERS][ITEMS];
new playerStoreLimitsGlobal[MAX_PLAYERS];

enum storeLimitCheck{
	slcTrue,
	slcNoMore,
	slcFalse
}
enum storeMneuItems{
	pmiAdd=1,
	pmiTake
}
/*
	This method sets how many items of specified type player can have in his store.
	If you do not want to limit this set limit as STORE_NO_LIMIT
	If you want to set global limit set item to STORE_LIMIT_GLOBAL
*/
stock td_set_player_store_limit(id, limit, item=STORE_LIMIT_GLOBAL){
	if(item == STORE_LIMIT_GLOBAL)
		playerStoreLimitsGlobal[id] = limit;
	else
		playerStoreLimits[id][item] = limit;
}


/*
	This method adds item to players store
*/
stock td_add_to_store(id, item){
	playerStoreItems[id][item]++;
}

/*
	This method sets count of item in players store
*/
stock td_set_store(id, item, count){
	playerStoreItems[id][item] = count
}

/*
	This method takes item from poekct
*/
stock td_take_from_store(id, item){
	if(!td_can_get_item_from_store(id, item))
		return false;
	
	if(playerStoreItems[id][item]) {
		playerStoreItems[id][item]--;
		td_custom_item_get_from_store(id, item);
		return true;
	} else {
		client_print(id, print_chat, "%s %L", PREFIX_DISP, id, "NOT_IN_STORE", id, ITEM_NAMES[item]);
	}
	return false;
}
/*
	This method checks if player can add items to store (global limit)
*/
stock storeLimitCheck:td_can_add_to_store(id){
	new limit = playerStoreLimitsGlobal[id];
	switch(limit) {
		case STORE_NO_LIMIT: return slcTrue;
		case 0: return slcFalse;
	}
	
	 if(limit > td_get_player_store_items_count(id))
		return slcTrue;
		
	return slcNoMore;
}
/*
	This method returns coutn of all items in players store
*/
stock td_get_player_store_size(id){
	new count = 0;
	for(new item=0;item<ITEMS;item++)
		count += playerStoreItems[id][item];
	return count
}

/*
	This method checks if item can be added to store (item limit)
*/
stock storeLimitCheck:td_can_add_item_to_store(id, item){
	new limit = playerStoreLimits[id][item];
	switch(limit) {
		case STORE_NO_LIMIT: return slcTrue;
		case 0: return slcFalse;
	}
	 if(limit > playerStoreItems[id][item])
		return slcTrue;
		
	return slcNoMore;
}

/*
	This displays menu with items player can take from store
*/
stock td_take_from_store_menu(id) {
	new text[64];
	new len = charsmax(text);
	formatex(text,  len, "/r%L", id, "TITLE_TAKE_FROM_STORE_MENU" );
	new menu = menu_create(text, "take_from_store_menu_handler");
	new info[1];
	
	new bool:canTake = td_get_player_store_size(id) > 0;
	
	if(!canTake){
		client_print(id, print_chat, "%s %L", PREFIX_DISP, id, "NO_ITEMS_IN_STORE");
		return;
	}
	new count;
	for(new item=0;item<ITEMS;item++){
		count = playerStoreItems[id][item];
		if(count){
			text[0] = '^0';
			formatex(text, len, "%L (%d)", id, ITEM_NAMES[item],  count);
			info[0] = item;
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);
}

/*
	Handler for taking items from store
*/
public take_from_store_menu_handler(id, menu, menuItem){
	if(menuItem == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, menuItem, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	new item = info[0];
	td_take_from_store(id, item);
	td_take_from_store_menu(id);
}
